package com.gitee.common.data.tree.node;

public abstract class OneNextTreeItemNodeImpl extends AbstractTreeItemNode implements OneNextTreeItemNodeI {

    private TreeItemNodeI next;

    @Override
    public int getNextLength() {
        return next == null ? 0 : 1;
    }

    @Override
    public TreeItemNodeI getNext() {
        return next;
    }

    @Override
    public TreeItemNodeI changeNext(TreeItemNodeI newer) {
        TreeItemNodeI older = next;
        next = newer;
        return older;
    }

    @Override
    public TreeItemNodeI searchNext(String id) {
        if (next == null || id == null) {
            return null;
        }
        if (id.equals(next.getId())) {
            return next;
        }
        if (next instanceof NextTreeItemNodeI) {
            return ((NextTreeItemNodeI) next).searchNext(id);
        }
        return null;
    }

    @Override
    public void append(TreeItemNodeI next) {
        this.next = next;
    }

    @Override
    public void deleteNext(TreeItemNodeI node) {
        if (node != null && this.next != null && this.next.compareTo(node) == 0) {
            this.next = null;
        }
    }
}
